package com.lb.jwitter.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;

import com.lb.jwitter.service.ConditionReport;
import com.lb.jwitter.service.TblCTDTNhomMHTuChon;
import com.lb.jwitter.service.TblCTDTNhomMHTuChonMonHoc;
import com.lb.jwitter.service.TblCTDTNhomMHTuChonMonHocService;
import com.lb.jwitter.service.TblDKMHDotDKHocVien;
import com.lb.jwitter.service.TblDKMHDotDKHocVienService;
import com.lb.jwitter.service.TblDKMHTaiKhoanHocVien;
import com.lb.jwitter.service.TblDKMHTaiKhoanHocVienService;
import com.lb.jwitter.service.TblMenu;
import com.lb.jwitter.service.TblMenuService;
import com.lb.jwitter.service.TblQuyenChiTietService;
import com.lb.jwitter.service.TblQuyenMenu;
import com.lb.jwitter.service.TblQuyenMenuService;
import com.lb.jwitter.service.TblTaiKhoan;
import com.lb.jwitter.service.TblThongTinHocVienMonHoc;
import com.lb.jwitter.service.TblThongTinHocVienMonHocService;
import com.lb.jwitter.util.ApplicationContextProvider;
import com.lb.jwitter.util.CTDT;
import com.lb.jwitter.util.NhomMonHocTuChon;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

@ContextConfiguration(locations="classpath:beans.xml") 
public class GetCTDTHocVienAction {

	private TblDKMHTaiKhoanHocVien tblDKMHTaiKhoanHocVien;
	public TblDKMHTaiKhoanHocVien getTblDKMHTaiKhoanHocVien() {
		return tblDKMHTaiKhoanHocVien;
	}

	public void setTblDKMHTaiKhoanHocVien(
			TblDKMHTaiKhoanHocVien tblDKMHTaiKhoanHocVien) {
		this.tblDKMHTaiKhoanHocVien = tblDKMHTaiKhoanHocVien;
	}


	private CTDT ctdt;

	public CTDT getCtdt() {
		return ctdt;
	}

	public void setCtdt(CTDT ctdt) {
		this.ctdt = ctdt;
	}

	private List<TblThongTinHocVienMonHoc> tblThongTinHocVienMonHocs;

	public List<TblThongTinHocVienMonHoc> getTblThongTinHocVienMonHocs() {
		return tblThongTinHocVienMonHocs;
	}

	public void setTblThongTinHocVienMonHocs(
			List<TblThongTinHocVienMonHoc> tblThongTinHocVienMonHocs) {
		this.tblThongTinHocVienMonHocs = tblThongTinHocVienMonHocs;
	}

	private boolean popup = false;

	public boolean isPopup() {
		return popup;
	}

	public void setPopup(boolean popup) {
		this.popup = popup;
	}

	private boolean monHocFail = false;
	

	public boolean isMonHocFail() {
		return monHocFail;
	}

	public void setMonHocFail(boolean monHocFail) {
		this.monHocFail = monHocFail;
	}


	@Autowired // Injected by Spring 
	ApplicationContextProvider contextProvider; 
	 	 
	public String execute() throws Exception
	{
		try{

			Map session = ActionContext.getContext().getSession();
			tblDKMHTaiKhoanHocVien =  (TblDKMHTaiKhoanHocVien) session.get("taiKhoanHocVien");
		     if( tblDKMHTaiKhoanHocVien == null) return "noprivilege";
		     
		    ApplicationContext context = contextProvider.getApplicationContext(); 
		    
		    TblDKMHDotDKHocVienService tblDKMHDotDKHocVienService 
		    	= (TblDKMHDotDKHocVienService)  context.getBean("tbl_DKMH_DotDK_HocVienClient");
		    
		    TblCTDTNhomMHTuChonMonHocService tblCTDTNhomMHTuChonMonHocService =
		    		(TblCTDTNhomMHTuChonMonHocService) context.getBean("tbl_CTDT_NhomMHTuChon_MonHocClient");
		    
		    TblThongTinHocVienMonHocService tblThongTinHocVienMonHocService =
		    		(TblThongTinHocVienMonHocService) context.getBean("tbl_ThongTin_HocVien_MonHocClient");
		    
		    
		    ctdt = (CTDT) session.get("CTDT");
		        
	      //set nhom mon hoc tu chon
		    List<ConditionReport> cons = new ArrayList<ConditionReport>();
	      cons.clear();
	      if(tblDKMHTaiKhoanHocVien != null ){ 
		        ConditionReport con = new ConditionReport(); 
		        con.setCol("nhomMHTCId.khoiNganhId.chuyenNganhId.id");
		        con.setValue1(tblDKMHTaiKhoanHocVien.getHocVienId().getChuyenNganhId().getId());
		        con.setOperator("eq");
		        cons.add(con);	
		   }
	      if(tblDKMHTaiKhoanHocVien != null  ){ 
		        ConditionReport con = new ConditionReport(); 
		        con.setCol("nhomMHTCId.khoiNganhId.khoaHoc");
		        con.setValue1(tblDKMHTaiKhoanHocVien.getHocVienId().getKhoaHoc());
		        con.setOperator("eq");
		        cons.add(con);	
		   }
	      { 
		        ConditionReport con = new ConditionReport(); 
		        con.setCol("nhomMHTCId.id");
		        con.setValue1("asc");
		        con.setOperator("order");
		        cons.add(con);	
		   }
	
	      List<TblCTDTNhomMHTuChonMonHoc> nhomMHTuChonMonHocs = tblCTDTNhomMHTuChonMonHocService.findtblCTDTNhomMHTuChonMonHocByCondition(cons, 0, 0);
	      //--------------------------------------------
	      List<NhomMonHocTuChon> nhomMonHocTuChons = new ArrayList<NhomMonHocTuChon>();      
	      List<TblCTDTNhomMHTuChonMonHoc> monHocs = new ArrayList<TblCTDTNhomMHTuChonMonHoc>();
	      
	      Long oldId = new Long(0);
	      TblCTDTNhomMHTuChon oldNhom = null;
	      for(TblCTDTNhomMHTuChonMonHoc nhomTuChonMonHoc : nhomMHTuChonMonHocs){
	   	   if(!nhomTuChonMonHoc.getNhomMHTCId().getId().equals(oldId)){
	   		   System.out.println("NHOM: "+nhomTuChonMonHoc.getNhomMHTCId().getTen() );
	   		   //them vao CTDT
	   		   if(monHocs.size() > 0){
	   			   NhomMonHocTuChon nhomMonHocTuChon = new NhomMonHocTuChon();
	   			   nhomMonHocTuChon.setNhomTuChon(oldNhom);
	   			   nhomMonHocTuChon.setMonHocTuChons(monHocs);
	   			   
	   			   //them vao danh sach cac nhom
	   			   nhomMonHocTuChons.add(nhomMonHocTuChon);
	   		   }
	   		   
	   		   //set lai oldId
	   		   oldId = nhomTuChonMonHoc.getNhomMHTCId().getId();
	   		   oldNhom = nhomTuChonMonHoc.getNhomMHTCId();
	   		   
	   		   //set moi lai danh sach mon hoc
	   		   monHocs.clear();
	   		   monHocs.add(nhomTuChonMonHoc);
	   		   System.out.println("-- "+ nhomTuChonMonHoc.getMonHocId().getMa());
	   	   }else{
	   		   //them mon hoc
	   		   monHocs.add(nhomTuChonMonHoc);
	   		   System.out.println("-- "+ nhomTuChonMonHoc.getMonHocId().getMa());
	   		
	   		   //neu la phan tu cuoi cung
	   		   /*
	   		   if(nhomMHTuChonMonHocs.indexOf(nhomTuChonMonHoc) == nhomMHTuChonMonHocs.size() -1){
	   			   NhomMonHocTuChon nhomMonHocTuChon = new NhomMonHocTuChon();
	   			   nhomMonHocTuChon.setNhomTuChon(nhomTuChonMonHoc.getNhomMHTCId());
	   			   nhomMonHocTuChon.setMonHocTuChons(monHocs);
	   			   
	   			   nhomMonHocTuChons.add(nhomMonHocTuChon);
	   		   }*/
	   	   }
	      }
	      //----------them cai cuoi-------------
	      if(monHocs.size() > 0){
  			   NhomMonHocTuChon nhomMonHocTuChon = new NhomMonHocTuChon();
  			   nhomMonHocTuChon.setNhomTuChon(oldNhom);
  			   nhomMonHocTuChon.setMonHocTuChons(monHocs);
  			   
  			   //them vao danh sach cac nhom
  			   nhomMonHocTuChons.add(nhomMonHocTuChon);
  		   }
	      
	      
	      ctdt.setNhomMonHocTuChons(nhomMonHocTuChons);
	      
	      //-------------------
	      //ket qua hoc tap cua hoc vien
	      cons.clear();
	      { 
		        ConditionReport con = new ConditionReport(); 
		        con.setCol("hocVienId.id");
		        con.setValue1(tblDKMHTaiKhoanHocVien.getHocVienId().getId());
		        con.setOperator("eq");
		        cons.add(con);	
		   }
	      tblThongTinHocVienMonHocs = tblThongTinHocVienMonHocService.findtblThongTinHocVienMonHocByCondition(cons, 0, 0);
			
			return Action.SUCCESS;
		}catch(Exception e){
			e.printStackTrace();
			return Action.ERROR;
		}

	}


	
	
	
	
	
}
